<?php
namespace Test\Controller;
use Think\Controller;
class LoginController extends Controller {

    public function login()
    {
        $this->display();
    }

    public function login_from_outsite()
    {
        $outsite = $_GET['outsite'];
        switch($outsite)
        {
            case 'qq':
                $this->qq_login();
                break;
            case 'wx':
                $this->wx_login();
                break;
            case 'wb':
                $this->wb_login();
                break;
            default:
                return '请选择第三方登录方式';
        }
    }

    public function qq_login()
    {
        import('ORG.QQ.qqConnectAPI');
        $QC = new \QC();
        $QC->qq_login();
    }

    public function wx_login()
    {
        $this->display();$state = md5(uniqid(rand(), TRUE));
        session('wxstate',$state);   //状态
        $redirect_uri = urlencode('http://www.babygx.com/index/Login/wx_callback.html');
        $code_url = "https://open.weixin.qq.com/connect/qrconnect?appid=wx37dbea8b3e407e1c&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
        header('Location:'.$code_url);
    }

    public function wb_login()
    {
        import('ORG.Weibo.wbConnectAPI');
        $o = new \SaeTOAuthV2( WB_AKEY , WB_SKEY );
        $code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );

        header('Location:'.$code_url);
    }

    public function wx_callback(){
        //--------验证state防止CSRF攻击
        if($_GET['state'] != $_SESSION['wxstate']){
            $this->success('登录失败，请重新登录！',U('/Index/Login/login'));
        }else{
            $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx37dbea8b3e407e1c&secret=24e9171150da5be69e317042dbdb021a&code={$_GET['code']}&grant_type=authorization_code";
            $curl=curl_init();//初始化一个 cURL 对象
            curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);// 获取数据返回
            curl_setopt($curl, CURLOPT_TIMEOUT,500);//设置一个长整形数，作为最大延续多少秒
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
            curl_setopt($curl, CURLOPT_URL,$url);
            curl_setopt($curl, CURLOPT_BINARYTRANSFER,true) ;// 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
            $res=curl_exec($curl);
            curl_close($curl);
            $result = json_decode($res, true);

            $urlw="https://api.weixin.qq.com/sns/userinfo?access_token={$result['access_token']}&openid={$result['openid']}";
            $curlx=curl_init();//初始化一个 cURL 对象
            curl_setopt($curlx, CURLOPT_RETURNTRANSFER,true);// 获取数据返回
            curl_setopt($curlx, CURLOPT_TIMEOUT,500);//设置一个长整形数，作为最大延续多少秒
            curl_setopt($curlx, CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($curlx, CURLOPT_SSL_VERIFYHOST,false);
            curl_setopt($curlx, CURLOPT_URL,$urlw);
            curl_setopt($curlx, CURLOPT_BINARYTRANSFER,true) ;// 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
            $resw=curl_exec($curlx);
            curl_close($curlx);
            $resultw = json_decode($resw, true);

            dump($res);
            dump($resw);

            //业务代码不要del
            /*$openid = $resultw['unionid'];
            $db=M("personal");
            $where['wx_openid']=$openid;
            //判断此QQ是否注册 唯一标识符openid 在表里加了个qq_openid为唯一哈
            $isqq=$db->where($where)->find();
            if($isqq){

                //写入登陆状态
                session('id', $isqq['id']);//ID
                session('name', $isqq['user']);  //名
                session('type', 'personal');   //类别
                session('state',1);   //状态
                $this->success('登陆成功！',U('Member/wmember'));
            }
            else{
                $data['wx_openid']=$openid; //QQ登陆唯openid
                $data['user']=$resultw['nickname']; //用户名
                $data['nickname']=$resultw['nickname']; //网名
                $data['img_url']=$resultw['headimgurl']; //头像

                switch ($resultw['sex'])
                {
                    case 1:
                        $data['childsex']='男'; //性别
                        break;
                    case 2:
                        $data['childsex']='女'; //性别
                        break;
                    default:
                        $data['childsex']='未知'; //性别
                }

                $data['createtime']=time(); //注册时间
                $data['status']='1'; //用户状态为启用
                //如果用户名存在
                $name['user']=$resultw['nickname'];
                $isname=$db->where($name)->find();
                if($isname){
                    //用户名存在添加随机数
                    $data['user']=$resultw['nickname']."_".rand(1000,9999);
                }
                //判断是否写入成功
                if($id=$db->add($data)){
                    //写入登陆状态
                    session('id', $id);//ID
                    session('name', $data['user']); //名
                    session('type', 'personal');//类别
                    session('state',1);   //状态
                    $this->success('注册成功！',U('Member/wmember'));
                }else{
                    $this->error('注册失败！',U('Member/login'));
                }
            }*/

        }
    }










}